package com.phuc.app.vuivui.utils;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

public class Utils {

	public static void unbindDrawable(View view) {
		if (view.getBackground() != null) {
			view.getBackground().setCallback(null);
			view.setBackgroundDrawable(null);
		}
		
		if (view instanceof AdapterView) {
			AdapterView viewGroup = (AdapterView) view;
			for (int i = viewGroup.getChildCount() - 1; i > -1; i--) {
				unbindDrawable(viewGroup.getChildAt(i));
			}
			return;
		}
		
		if (view instanceof ViewGroup) {
			ViewGroup viewGroup = (ViewGroup) view;
			for (int i = viewGroup.getChildCount() - 1; i > -1; i--) {
				unbindDrawable(viewGroup.getChildAt(i));
			}
			viewGroup.removeAllViews();
		}
	}
	
	public static int makeRandomNumber(int range) {
		float ranNum = (float)Math.random();
		return (int)(ranNum * 1000000) % range;
	}
	
	/**
	 * Get image resource from name of image.
	 * @param name name of resource
	 * @param context the context
	 * @return the id of resource
	 */
	public static int getImageResourceFromName(String name, Context context) {
		int resId = context.getResources().getIdentifier(
				name, Constants.STR_DRAWABLE, context.getPackageName());
		return resId;
	}
	
	public static String convertHTML(String url){
    	return "<br /><img width=\"320\" src=\""+url+"\"/><br /><br />";
    }
}
